package com.winit.schedule.core.repository.entity;

import java.io.Serializable;

import org.springframework.util.StringUtils;

import com.winit.schedule.core.model.TaskID;
import com.winit.schedule.core.repository.constants.TaskItemStatus;

public class TaskItemEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID     = -4376719763798006801L;

    private TaskID            taskId;

    /**
     * 任务项ID
     */
    private String            taskItemId;

    /**
     * 任务项参数
     */
    private String            parameter;

    /**
     * 任务状态
     */
    private TaskItemStatus    taskItemStatus;

    /**
     * 任务运行器
     */
    private TaskRunnerEntity  taskRunner;

    /**
     * 任务项最后一次抓取/分配时间
     */
    private long              lastFetchTime        = 0L;

    /**
     * 任务项状态最后一次心跳更新时间
     */
    private long              lastStatusUpdateTime = 0L;

    /**
     * 最后抓取任务项的tasktracker uuid
     */
    private String            lastFetchTaskTrackerUuid;

    /**
     * 下一次运行时间
     */
    private long              nextRunTime;

    /**
     * 是否自动恢复状态异常的任务项;1:自动恢复，0：不自动恢复
     */
    private boolean           autoRecovery;

    public TaskItemEntity(){
    }

    public TaskItemEntity(TaskID taskId, String parameter, boolean autoRecovery){
        this.taskId = taskId;
        this.parameter = parameter;
        this.taskItemId = taskId + "$" + (StringUtils.isEmpty(parameter) ? '$' : parameter);
        this.taskItemStatus = TaskItemStatus.INACTIVIE;
        this.autoRecovery = autoRecovery;
    }

    public TaskItemEntity(TaskID taskId, String parameter, long nextRunTime, boolean autoRecovery){
        this.taskId = taskId;
        this.parameter = parameter;
        this.taskItemId = taskId + "$" + (StringUtils.isEmpty(parameter) ? '$' : parameter);
        this.taskItemStatus = TaskItemStatus.INACTIVIE;
        this.nextRunTime = nextRunTime;
        this.autoRecovery = autoRecovery;
    }

    public String getTaskItemId() {
        return taskItemId;
    }

    public void setTaskItemId(String taskItemId) {
        this.taskItemId = taskItemId;
    }

    public String getParameter() {
        return parameter;
    }

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public TaskItemStatus getTaskItemStatus() {
        return taskItemStatus;
    }

    public void setTaskItemStatus(TaskItemStatus taskItemStatus) {
        this.taskItemStatus = taskItemStatus;
    }

    public TaskRunnerEntity getTaskRunner() {
        return taskRunner;
    }

    public void setTaskRunner(TaskRunnerEntity taskRunner) {
        this.taskRunner = taskRunner;
    }

    public TaskID getTaskId() {
        return taskId;
    }

    public void setTaskId(TaskID taskId) {
        this.taskId = taskId;
    }

    public long getLastStatusUpdateTime() {
        return lastStatusUpdateTime;
    }

    public void setLastStatusUpdateTime(long lastStatusUpdateTime) {
        this.lastStatusUpdateTime = lastStatusUpdateTime;
    }

    public long getNextRunTime() {
        return nextRunTime;
    }

    public void setNextRunTime(long nextRunTime) {
        this.nextRunTime = nextRunTime;
    }

    public String getLastFetchTaskTrackerUuid() {
        return lastFetchTaskTrackerUuid;
    }

    public void setLastFetchTaskTrackerUuid(String lastFetchTaskTrackerUuid) {
        this.lastFetchTaskTrackerUuid = lastFetchTaskTrackerUuid;
    }

    public long getLastFetchTime() {
        return lastFetchTime;
    }

    public void setLastFetchTime(long lastFetchTime) {
        this.lastFetchTime = lastFetchTime;
    }

    public boolean isAutoRecovery() {
        return autoRecovery;
    }

    public void setAutoRecovery(boolean autoRecovery) {
        this.autoRecovery = autoRecovery;
    }

    public String toString() {
        return "TaskItemEntity[taskId:" + this.taskId.getId() + ", taskItemId:" + this.taskItemId + ", parameter:"
               + this.parameter + ", taskItemStatus:" + this.taskItemStatus + "]";
    }

}
